import Vue from 'vue'
import Router from 'vue-router'

import OverView from '@/views/report/OverView'
import overView1 from '@/views/report/OverView.test'
import Bind from '@/views/report/Bind'
import CompanyList from '@/views/report/CompanyList'
import Test from '@/views/report/Test'
import TestList from '@/views/report/TestList'
import noCompany from '@/views/report/noCompany'

import store from '@/store/report/index'
import api from '@/api/base.api'
import {
    routesObj,
    beforeEachHandler
} from '@/router/config'

Vue.use(Router)
var router = new Router({
    routes: [{
            /* 默认首页 */
            path: '/',
            name: 'home',
            component: CompanyList
        },
        {
            /* 当前openid绑定企业列表 */
            path: '/companyList',
            name: 'companyList',
            component: CompanyList
        },
        {
            /* 新LIST测试 */
            path: '/list',
            name: 'List',
            component: CompanyList
        },
        {
            /* 新LIST测试 */
            path: '/testList',
            name: 'testList',
            component: TestList
        },
        {
            /* 新overView测试 */
            path: '/overView1',
            name: 'overView11',
            component: overView1
        },
        {
            /* 报表详情 */
            path: '/overView/:companyCode/:userCode',
            name: 'overView',
            component: OverView
        },
        {
            /* openid和企业关联页面 */
            path: '/bind',
            name: 'bind',
            component: Bind
        },
        {
            /* 测试页面 */
            path: '/noCompany',
            name: 'noCompany',
            component: noCompany
        },
        {
            /* 测试页面 */
            path: '/test',
            name: 'test',
            component: Test
        },
        routesObj
    ]
})
/* 获取openid */
router.beforeEach(beforeEachHandler);
/* 
1.其他页面不用考虑用户刷新问题，因为其他依赖openid，openid会统一处理。
2.companyList页面需要做一个简单的递归（因为只有这个页面需要调用接口查询公司信息）
3.尽量不做全局的拦截，根据to.fullPath做判断，场景会更少
4.是否绑定和公司信息这里处理有点不清晰，后期需改进。因为其他任何页面都要判断是否绑定。

思考：是否不以companyList作为核心点？
1.做一个列表页，mounted以后做自动跳转
2.入口处做逻辑判断
3.重新做个钩子做异步查询，又要兼顾其他页面
*/
/* 判断是否绑定 */

router.beforeEach(function (to, from, next) {
    if (to.fullPath == '/bind' || to.fullPath == '/overView1' || to.fullPath == '/testList') { //同刷新回首页。next机制要是要理解
        next();
    } else {
        if (store.state.isBind) {
            next();
        } else {
            api.getInfo().then(rtn => {

                if (rtn.data.flag) {
                    store.commit('SET_FLAG', true);
                    store.commit('UPDATE_COMPANYLIST', rtn.data.companys);
                    next();
                } else {
                    store.commit('SET_FLAG', false);
                    store.commit('UPDATE_COMPANYLIST', null);
                    next('/bind');
                }
            })
        }
    }
});

/* 
router.beforeEach(function (to, from, next) {
    if (to.fullPath == '/companyList') {
        //* 没有查询绑定信息之前
        if (store.state.companyList == null) {

            api.getInfo().then(rtn => {
                console.log(rtn);
                if (rtn.code === 200 && rtn.data.flag === true) {
                    store.commit('SET_FLAG', true);
                    store.commit('UPDATE_COMPANYLIST', rtn.data.companys);
                    next("/companyList"); //类似一次性的递归
                } else {
                    next('/bind');
                }
            })
        }
        // 绑定且只有一个公司
        else if (store.state.isBind && store.state.companyList.length == 1) {
            var path = `/overView/${store.state.companyList[0].companyCode}/${store.state.companyList[0].holderCode}`;
            next(path);
        }
        //绑定且多个公司
        else {
            next();
        }
    } else {
        next();
    }
})
 */
export default router